package com.faya.green.service.impl;

import com.faya.green.enumeration.StatusEnum;
import com.faya.green.model.LogInfo;
import com.faya.green.service.LogInfoService;
import com.faya.green.utils.CommonUtils;
import com.faya.green.utils.HttpUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.UUID;

/**
 * 用户 ：LX
 * 创建时间： 2018/7/13. 11:19
 * 地点：广州
 * 目的: 系统访问日志
 * 结果：
 */
@Service
public class LogInfoServiceImpl implements LogInfoService {


    @Transactional(rollbackFor = Exception.class)
    @Override
    public LogInfo addLogInfoSemifinished (String logName){
        //记录登录信息
        LogInfo logInfo = new LogInfo();
        logInfo.setId(UUID.randomUUID().toString());
        logInfo.setName(logName);
        logInfo.setTime(new Date());
        logInfo.setCreateTime(new Date());
        logInfo.setUpdateTime(new Date());
        logInfo.setStatus(StatusEnum.STATUS_ADD.getCode());
        //获取IP 浏览器等信息
        HttpServletRequest request = CommonUtils.getRequest();
        logInfo.setIpaddr(HttpUtil.getIpAddr(request));
        logInfo.setOs(HttpUtil.getOs(request));
        logInfo.setBrowser(HttpUtil.getBrowserInfo(request));
        logInfo.setLocation(HttpUtil.getIpChineByTaoBaoAPI(logInfo.getIpaddr()));

        return logInfo;
    }
}
